package a66weding.com.jiehuntong.activity.home.hotel.shop;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.xutils.x;

import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.LoginActivity;
import a66weding.com.jiehuntong.adapter.MyBaseAdapter;
import a66weding.com.jiehuntong.beans.CancelCollectBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HomeBotomListBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.MeShoppingCartDeleteBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.utils.ImageOptionsUtil;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;
import a66weding.com.jiehuntong.view.ConfirmLoginDialog;

/**
 * Created by admin on 2016/9/14.
 */
public class HotPackageAdapter extends MyBaseAdapter implements OnNetResultListener{


    public HotPackageAdapter(Context context, List data) {
        super(context, data);

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder viewHolder ;
        if (convertView==null){
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_homedetail_tabright,null);
            viewHolder.iv_homebusitem_business = (ImageView) convertView.findViewById(R.id.iv_homebusitem_business);
            viewHolder.rl_me_collect = (RelativeLayout) convertView.findViewById(R.id.rl_me_collect);
            viewHolder.tv_homecustom_talk = (TextView) convertView.findViewById(R.id.tv_homecustom_talk);
            viewHolder.tv_homebusitem_hotalname = (TextView) convertView.findViewById(R.id.tv_homebusitem_hotalname);
            viewHolder.tv_homelist_itemprice = (TextView) convertView.findViewById(R.id.tv_homelist_itemprice);
            viewHolder.tv_homelist_itemmarketprice = (TextView) convertView.findViewById(R.id.tv_homelist_itemmarketprice);
            viewHolder.iv_homebus_markimg = (ImageView) convertView.findViewById(R.id.iv_homebus_markimg);
            viewHolder.iv_home_collect_show = (ImageView) convertView.findViewById(R.id.iv_home_collect_show);
            viewHolder.iv_home_collect = (ImageView) convertView.findViewById(R.id.iv_home_collect);
            viewHolder.rlay_homebus_img = (RelativeLayout) convertView.findViewById(R.id.rlay_homebus_img);
            viewHolder.ll_home_particulars = (LinearLayout) convertView.findViewById(R.id.ll_home_particulars);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final HomeBotomListBean.DataBean dataBean = (HomeBotomListBean.DataBean) data.get(position);
        final boolean collect = dataBean.isIsCollection();
        x.image().bind(viewHolder.iv_homebusitem_business,dataBean.getNvc_PicMain(), ImageOptionsUtil.getOptions316_720());
        viewHolder.tv_homecustom_talk.setText(dataBean.getBusiness().getNvc_StoreName());
        viewHolder.tv_homebusitem_hotalname.setText(dataBean.getNvc_Name());
        viewHolder.tv_homelist_itemprice.setText(""+dataBean.getDm_ShopPrice());
        viewHolder.tv_homelist_itemmarketprice.setText("¥"+dataBean.getDm_Price());
        viewHolder.tv_homelist_itemmarketprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
        x.image().bind(viewHolder.iv_homebus_markimg,dataBean.getBusiness().getNvc_Logo(), ImageOptionsUtil.getcircle_fixy());

        if (collect) {  //如果收藏过就显示红心
            viewHolder.iv_home_collect.setVisibility(View.GONE);
            viewHolder.iv_home_collect_show.setVisibility(View.VISIBLE);
        } else {        //如果没有收藏就显示白心
            viewHolder.iv_home_collect.setVisibility(View.VISIBLE);
            viewHolder.iv_home_collect_show.setVisibility(View.GONE);
        }
        //收藏
        viewHolder.rl_me_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ((((HomeProductdetailActivity)context).isLogin())){
                    if (collect){//收藏
                        cancelAttention(dataBean);
                    }else{
                        addAttention(dataBean);
                    }
                }else{
                    showLoginDialog(context, "亲，需要登录哦~", "去登录", "取消");
                }
            }
        });
        //点击图片跳转到商品详情页
        viewHolder.ll_home_particulars.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i_id = dataBean.getI_Id();
                Intent intent = new Intent(context, HomeProductdetailActivity.class);
                intent.putExtra("productId",i_id);
                context.startActivity(intent);
            }
        });

        return convertView;
    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        if(data instanceof MeShoppingCartDeleteBean){      // 加入收藏成功后
            MyDialog myDialog = new MyDialog(context,"收藏成功!");
            myDialog.show();
        }else if(data instanceof CancelCollectBean){      // 删除收藏成功后
            MyDialog myDialog = new MyDialog(context,"取消收藏成功!");
            myDialog.show();
        }

    }

    @Override
    public void onError(ErrorBean errorBean) {

    }

    public class ViewHolder{
        //图片
        ImageView iv_homebusitem_business;
        //收藏
        RelativeLayout rl_me_collect;
        //标题
        TextView tv_homecustom_talk;
        //店铺名称
        TextView tv_homebusitem_hotalname;
        //售价
        TextView tv_homelist_itemprice;
        //原价
        TextView tv_homelist_itemmarketprice;
        //logo图像
        ImageView iv_homebus_markimg;
        //红心
        ImageView iv_home_collect;
        //白心
        ImageView iv_home_collect_show;
        //点击跳转界面布局
        RelativeLayout rlay_homebus_img;
        //整个布局
        LinearLayout ll_home_particulars;

    }

    //取消收藏
    private void cancelAttention( HomeBotomListBean.DataBean dataBean){
        String oldUrl= UrlDealUtil.getRealUrlNewIdApi(RequestCode.POSTCOLLECTIONCANCEL);
        String url = oldUrl + "&productID=" + dataBean.getI_Id();
        HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url,
                (HomeProductdetailActivity) context, CancelCollectBean.class, RequestCode.POSTCOLLECTIONCANCEL);
        HttpHandleUtil.executeCommonPost(context,httpRequestBean);
        dataBean.setIsCollection(false);
        notifyDataSetChanged();
    }


    //添加收藏
    private void addAttention( HomeBotomListBean.DataBean dataBean){
        String oldUrl= UrlDealUtil.getRealUrlNewIdApi(RequestCode.POSTCOLLECTIONCREATE);
        String url = oldUrl + "&productID=" + dataBean.getI_Id() + "&businessID=" + dataBean.getI_Business();
        HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url,
                (HomeProductdetailActivity) context, MeShoppingCartDeleteBean.class, RequestCode.POSTCOLLECTIONCREATE);
        HttpHandleUtil.executeCommonPost(context,httpRequestBean);
        dataBean.setIsCollection(true);
        notifyDataSetChanged();
    }


    /**
     * 自定义的对话框
     */
    class MyDialog extends Dialog {
        private String title;
        public MyDialog(Context context, String title) {
            super(context);
            this.title = title;
        }
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //设置不显示对话框标题栏
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            //设置对话框显示哪个布局文件
            setContentView(R.layout.pop_tocollect);
            TextView titleTv = (TextView) findViewById(R.id.title);
            titleTv.setText(title);
        }
    }

    /**
     * 登录对话框
     * @param context
     * @param title
     * @param confirm
     * @param cancel
     */
    public  void showLoginDialog(final Context context, String title, String confirm, String cancel) {
        final ConfirmLoginDialog confirmDialog = new ConfirmLoginDialog(context, title, confirm, cancel,R.layout.pop_tologin);
        confirmDialog.show();
        confirmDialog.setClicklistener(new ConfirmLoginDialog.ClickListenerInterface() {
            @Override
            public void doConfirm() {
                Intent intent = new Intent(context, LoginActivity.class);
                ((HomeProductdetailActivity)context).finish();
                context.startActivity(intent);
                confirmDialog.dismiss();
            }
            @Override
            public void doCancel() {
                confirmDialog.dismiss();
            }
        });
    }
}
